<?php

/**
 * @file formbuttons.php
 * @brief Clase para generar un boton de formularios
 * @author DyrTechnologies
 * @version V 2.0
 * @date Wed Oct 03 2007
 *
 *  Part of the CEFIRO dynamic web framework project
 *
 *  Copyright (C) 2007  Dyr technologies http://dyrtech.com.ve
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @class Clase formbuttons
 */

require_once(CEF_BUTTON3D);

class formbuttons {

	private $left_button3d;
	private $right_button3d;
	private $sec_step;
	private $name;
  private $buttonCaptions;
  private $opt;
  private $showRightButton;

	/**
	 * @brief Constructor por omision y parametrico de la clase aprovechamos el hecho de que cada atributo tiene un valor por omision predefinido para no rehacer el constructor
	 *
	 * @param string $_name_form Recuerde nombrar este widget con el nombre del formulario al que pertenece, y en la plantilla meter todos los demás controles dentro de una tabla que se llame 'table_'.$_name_form pero con éste fuera.
	 * @param string $_opt Opcion a la que va con el proximo submit
	 * @param bool $_showRB Mostrar el botón derecho?
	 * @return formbuttons La instancia de la clase
	 */
	public function formbuttons($_name_form, $_opt = '', $_showRB = true, $_sec = false, $_right_opt = '', $_buttonCaptions = null){
		$this->name = $_name_form;
		$this->opt = $_opt;
		$this->showRightButton = $_showRB;
		$this->set_sec($_sec, $_right_opt, $_buttonCaptions);
	}


	/**
	 * @brief Las funciones get_VAR devuelven las variables que tenga ajustadas la instancia: Nombre (name), Boton derecho (right_button3d), Boton Izquierdo (left_button3d)
	 *
	 * @return string Valor de VAR
	 */
	public function get_buttonCaptions(){
		return $this->buttonCaptions;
	}

	public function get_name(){
		return $this->name;
	}

	public function get_sec(){
		return $this->sec_step;
	}

	public function get_right_button(){
		return $this->right_button3d->get_button3d();
	}

	public function get_right_type(){
		return $this->right_button3d->get_type();
	}

	public function get_right_text(){
		return $this->right_button3d->get_text();
	}

	public function get_right_opt(){
		return $this->right_button3d->get_opt();
	}

	public function get_left_button(){
		return $this->left_button3d->get_button3d();
	}

	public function get_left_type(){
		return $this->left_button3d->get_type();
	}

	public function get_left_text(){
		return $this->left_button3d->get_text();
	}

	public function get_left_opt(){
		return $this->left_button3d->get_opt();
	}

	/**
	 * @brief Las funciones set_VAR ajustan las variables de la instancia: Direccion del Hiperenlace (href), Nombre (name), Texto (text), opciones de entrada (opt)
	 */
	public function set_buttonCaptions($_buttonCaptions){
		$this->buttonCaptions=$_buttonCaptions;
	}

	public function set_sec($_sec = false, $_right_opt = '', $_buttonCaptions = null){
	  if (isset($_buttonCaptions))
	     $this->set_buttonCaptions($_buttonCaptions);
	  $form_name = $this->name;
		if($_sec) {
			$this->sec_step = "true";
  		$left_opt = new input_opt();
  		$left_opt->set_onClic("zajax.left2('$form_name')");
  		$right_opt = new input_opt();
  		if ($_right_opt == '')
  		  $right_opt->set_onClic("zajax.right2('$form_name')");
		  else
  		  $right_opt->set_onClic("zajax.$('opt').value='$_right_opt';zajax.left2('$form_name')");
  		$this->left_button3d=new button3d("left_$form_name",$this->buttonCaptions[2],'button',$left_opt);
  		$this->right_button3d=new button3d("right_$form_name",$this->buttonCaptions[3],'button',$right_opt);
		} else {
			$this->sec_step = "false";
  		$left_opt = new input_opt();
  		$left_opt->set_onClic("zajax.left1('$form_name')");
  		$right_opt = new input_opt();
  		if ($_right_opt == '')
  		  $right_opt->set_onClic("zajax.right1('$form_name')");
		  else
  		  $right_opt->set_onClic("zajax.$('opt').value='$_right_opt';zajax.left2('$form_name')");
  		$this->left_button3d=new button3d("left_$form_name",$this->buttonCaptions[0],'button',$left_opt);
  		$this->right_button3d=new button3d("right_$form_name",$this->buttonCaptions[1],'button',$right_opt);
		}
	}

	public function set_right_type($_type){
		$this->right_button3d->set_type($_type);
	}

	public function set_right_text($_text){
		$this->right_button3d->set_text($_text);
	}

	public function set_right_opt($_opt){
		$this->right_button3d->set_opt($_opt);
	}

	public function set_left_type($_type){
		$this->left_button3d->set_type($_type);
	}

	public function set_left_text($_text){
		$this->left_button3d->set_text($_text);
	}

	public function set_left_opt($_opt){
		$this->left_button3d->set_opt($_opt);
	}

	/**
	 * @brief Ajusta los valores de smarty de acuerdo a lo especificado por los atributos de la instancia formbuttons
	 */
	private function smarty_formbuttons(){
		global $smarty;
		$smarty->assign("leftbutton",$this->left_button3d->display_button3d(true));
		if ($this->showRightButton)
		  $smarty->assign("rightbutton",$this->right_button3d->display_button3d(true));
		$smarty->assign("opt",$this->opt);
		$smarty->assign("sec_step",$this->get_sec());
		$smarty->assign("name",$this->get_name());
		$smarty->assign("but_caps",$this->buttonCaptions);
	}

	/**
	 * @brief Asigna el formbutton a la variable $smarty para su renderizacion
	 *
	 */
	public function display_formbuttons($fetch = false){
		global $smarty;
		$this->smarty_formbuttons();
		$smarty->template_dir = dirname(__FILE__).'/';
		if ($fetch)
		  return $smarty->fetch('formbuttons.tpl');
	  else
		  $smarty->assign( $this->name , $smarty->fetch('formbuttons.tpl') );
	}
}

?>